import { createSlice,createAsyncThunk } from '@reduxjs/toolkit';
import { findById } from '../../services/student';

export const fetchStudent = createAsyncThunk("student/fetchStudent",async (id) => {
    let data = await findById(id);
    return data;
});

const studentSlice = createSlice({
    name: "student",
    initialState: {
        isUpdateOpen:false,
        student:{}
    },
    reducers:{
        // reducer方法要求必须不能有任何副作用effect
        // 无副作用函数是指函数执行后不会对外部变量有任何影响，
        // 并且该函数中不能执行任何异步操作
       setIsUpdateOpen(state,action){
            state.isUpdateOpen = action.payload;
       },
       setStudent(state,action){
            state.student = action.payload;
       }
    },
    extraReducers(builder){
        builder.addCase(fetchStudent.fulfilled, (state, action) => {
            state.student = action.payload;
            console.log("state.student",state.student)
        })
    }
});
// 将切片自动生成的action导出，生成action的名字和reducer的名字是一样的
export const {setIsUpdateOpen,setStudent} = studentSlice.actions;
// 将切片中的reducer导出
export default studentSlice.reducer;
